package com.gaara.test;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;


/**
 *  多线程类
 *
 */
public class MyThread extends Thread {

	private Paint paint ;  
	private Canvas canvas; 
	private SurfaceHolder sfh;

	private IScene scene;

	/*	private static MyThread myThread;
	public static MyThread getInstance(){
		if(myThread == null){
			myThread = new MyThread();
		}

		return myThread;
	}*/

	public IScene getScene() {
		return scene;
	}

	public void setScene(IScene scene) {
		if(this.scene !=null){
			this.scene.release();
		}
		this.scene = scene;
	}

	public MyThread(){
		super("thread");

		paint = new Paint();   
		paint.setAntiAlias(true);//设置画笔无锯齿(如果不设置可以看到效果很差)   
	}

	public void setSureface(SurfaceHolder sfh ){
		this.sfh = sfh;
	}

	public void onTouchEvent(MotionEvent event) {
		if(scene.isInit()){
			scene.onTouchEvent(event);
		}
	}

	public void run() {
		while(true){
			try {     
				canvas = sfh.lockCanvas(); // 得到一个canvas实例     
				if( scene != null){
					scene.update(canvas);
				}

			} catch (Exception ex) {     
			} finally { // 备注3     
				if (canvas != null)     
					sfh.unlockCanvasAndPost(canvas);  // 将画好的画布提交     
			} 
		}
	}

}
